package slh.Book;

public class Book {
    private int bookId;
    private String title;
    private String author;
    private String publisher;
    private int publishYear;
    private int pages;
    private String language;
    private String translator;
    private String description;
    private String coverImage;
    private double score;        // 评分(1.0-10.0)
    private int ratingCount;     // 评分人数

    // 构造方法
    public Book() {
    }

    public Book(String title, String author, String publisher, int publishYear,
                int pages, String language, String translator,
                String description, String coverImage) {
        this.title = title;
        this.author = author;
        this.publisher = publisher;
        this.publishYear = publishYear;
        this.pages = pages;
        this.language = language;
        this.translator = translator;
        this.description = description;
        this.coverImage = coverImage;
        this.score = 0.0;       // 默认评分0.0(表示暂无评分)
        this.ratingCount = 0;    // 默认评分人数0
    }

    // Getter 和 Setter 方法
    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public int getPublishYear() {
        return publishYear;
    }

    public void setPublishYear(int publishYear) {
        this.publishYear = publishYear;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getTranslator() {
        return translator;
    }

    public void setTranslator(String translator) {
        this.translator = translator;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getCoverImage() {
        return coverImage;
    }

    public void setCoverImage(String coverImage) {
        this.coverImage = coverImage;
    }

    // 新增的评分相关方法
    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        // 确保评分在1.0-10.0范围内
        this.score = Math.max(1.0, Math.min(10.0, score));
    }

    public int getRatingCount() {
        return ratingCount;
    }

    public void setRatingCount(int ratingCount) {
        // 确保评分人数不为负数
        this.ratingCount = Math.max(0, ratingCount);
    }

    // 添加一个便捷方法来更新评分
    public void addRating(double newScore) {
        double totalScore = this.score * this.ratingCount + newScore;
        this.ratingCount++;
        this.score = totalScore / this.ratingCount;
    }

    // 可选：获取格式化评分(保留1位小数)
    public String getFormattedScore() {
        return ratingCount > 0 ? String.format("%.1f", score) : "暂无评分";
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookId=" + bookId +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", publisher='" + publisher + '\'' +
                ", publishYear=" + publishYear +
                ", pages=" + pages +
                ", language='" + language + '\'' +
                ", translator='" + translator + '\'' +
                ", description='" + description + '\'' +
                ", coverImage='" + coverImage + '\'' +
                ", score=" + score +
                ", ratingCount=" + ratingCount +
                '}';
    }
}